<template>
  <a-modal
    :maskClosable="false"
    :destroyOnClose="true"
    title="缴费提示"
    centered
    :width="400"
    v-model="visible"
    @ok="handleOk"
  >
    <div class="content">
      确认调整会员信息？<a class="a" v-if="!isPayment" @click="handleExport"
        >查看{{ type === 'memberSet' ? '缴费函' : '续费函' }}</a
      >
    </div>
    <SuccessModal
      ref="successModal"
      title="会员信息已调整"
      :close="closeSuccessModal"
      :confirm="confirmSuccessModal"
      :confirmText="`发${getTypeText}${this.form.state ? '成功' : ''}通知`"
    />
  </a-modal>
</template>

<script>
import SuccessModal from '@/components/SuccessModal/index.vue'

import { PayFeesCreatePaymenrecord, PaymentExport, RenewCreatePaymenrecord } from '@/api/member/member'
import moment from 'moment'
export default {
  components: {
    SuccessModal,
  },
  data() {
    return {
      visible: false,
      form: {},
      type: 'memberSet',
      isPayment: false,
      userData: {},
    }
  },
  computed: {
    getTypeText() {
      let type = this.type === 'memberSet' ? '缴费' : '续费'
      return type
    },
  },
  methods: {
    show(form, type, isPayment) {
      this.form = form
      this.type = type
      this.isPayment = isPayment
      this.visible = true
    },
    closeModal() {
      this.visible = false
      this.$emit('success')
    },
    closeSuccessModal() {
      this.$refs.successModal.closeModal()
      this.closeModal()
    },
    confirmSuccessModal() {
      let ids = [2, 11]
      let data = [
        {
          name: '续费通知',
          associatedObjects: null,
          customType: 0,
          dataSource: 'memberList',
          parentId: 2,
          nodeList: null,
          id: 11,
        },
      ]
      let pageSelectedObj = {
        1: [
          {
            userId: this.userData.userId,
            companyName: this.userData.companyName,
            userName: this.userData.leadingName,
            phone: this.userData.leadingPhone,
            companyPosition: this.userData.leadingSocialPosition,
            memberLevelName: this.userData.nowLevelName,
            isOfficialAccount: this.userData.isOfficialAccount,
            // 打标签使用,由于会员table,标签用的ID非唯一KEY,无法全选回显勾选,
            // 所以使用pageSelectedObj对象获取
            // appUserInfoId: item.appUserInfoId,
            applyType: this.userData.applyType,
          },
        ],
      }
      if (this.type === 'memberSet') {
        ids = [2, 16]

        data = [{ id: 16, name: '入会缴费通知', dataSource: 'applyUserList' }]
      }
      this.$store.commit('SET_SMSTEMPLATEOBJ', {
        ids: ids,
        data: data,
      })
      this.$store.commit('SET_PAGESELECTEDOBJ', pageSelectedObj)
      this.$router.push({
        path: '/operations/createMessage',
      })
    },
    async handleExport() {
      let { proposer, levelName, nowLevelName, membershipFees, payMoney, applyType, startTime, endTime, allottedTime } =
        this.form
      startTime = moment(startTime).format('YYYY-MM-DD')
      endTime = moment(endTime).format('YYYY-MM-DD')
      let path = (this.type === 'memberSet' && 'paymentExport') || 'renewExport'
      let res = await PaymentExport(
        {
          memberName: proposer,
          levelName,
          nowLevelName,
          membershipFees,
          amountPaid: payMoney,
          applyType,
          startTime,
          endTime,
          unit: allottedTime / 12 + '年',
          allottedTime,
        },
        path
      )
      if (res && res.code != 0) this.$message.error(res.msg)
    },
    async handleOk() {
      let res = {}
      let { applyId, levelId, paymentVoucher, startTime, endTime, payMoney, state } = this.form
      if (this.type === 'memberSet')
        res = await PayFeesCreatePaymenrecord({
          ...this.form,
          memberOtherSettingsDto: {
            ...this.form.memberOtherSettingsDto,
            show: this.form.memberOtherSettingsDto.show ? 1 : 0,
          },
        })
      else
        res = await RenewCreatePaymenrecord({
          memberId: applyId,
          levelId,
          paymentVoucher,
          startTime,
          endTime,
          payMoney,
          state,
        })

      if (this.form.state === 1) {
        this.$message.success('设置成功')
        this.closeModal()
      } else {
        this.userData = res.data
        this.$refs.successModal.showModal()
      }
    },
  },
}
</script>

<style scoped>
.content {
  padding-bottom: 20px;
}
</style>
